package com.ruirenyun.service.impl;

import com.ruirenyun.entity.MobileUser;
import com.ruirenyun.mapper.MobileUserEsMapper;
import com.ruirenyun.mapper.MobileUserMapper;
import com.ruirenyun.service.MobileUserService;
import org.elasticsearch.common.lucene.search.function.CombineFunction;
import org.elasticsearch.common.lucene.search.function.FunctionScoreQuery;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.query.functionscore.FunctionScoreQueryBuilder;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.data.elasticsearch.core.query.SearchQuery;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * 手机端用户表(MobileUser)表服务实现类
 *
 * @author makejava
 * @since 2021-04-20 11:47:10
 */
@Service
public class MobileUserServiceImpl implements MobileUserService {
    @Resource
    private MobileUserMapper mobileUserMapper;
    @Resource
    private MobileUserEsMapper mobileUserEsMapper;

    @Override
    public List<MobileUser> getMobileUserList(String keyword, int start, int size) {
        initDatabaseToES();
        BoolQueryBuilder queryBuilder = QueryBuilders.boolQuery();
        FunctionScoreQueryBuilder.FilterFunctionBuilder[] filterFunctionBuilders = new FunctionScoreQueryBuilder.FilterFunctionBuilder[3];
        FunctionScoreQueryBuilder functionScoreQueryBuilder = QueryBuilders.functionScoreQuery(queryBuilder,filterFunctionBuilders)
                //.filterFunctionBuilders(QueryBuilders.matchPhraseQuery("name",keyword), ScoreFunctionBuilders.weightFactorFunction(100))
                .scoreMode(FunctionScoreQuery.ScoreMode.SUM).boostMode(CombineFunction.SUM)
                .setMinScore(10);
        Sort sort = Sort.by(Sort.Direction.DESC,"mobileUserId");
        Pageable pageable = PageRequest.of(start,size,sort);
        //es搜索默认第一页页码是0
        SearchQuery searchQuery = new NativeSearchQueryBuilder()
                .withPageable(pageable)
                .withQuery(functionScoreQueryBuilder)
                .build();
        Page<MobileUser> page = mobileUserEsMapper.search(searchQuery);

        return page.getContent();
    }

    @Override
    public MobileUser getMobileUserById(Long userId) {
        return mobileUserMapper.selectByPrimaryKey(userId);
    }

    /**
     * 初始化数据到es. 因为数据刚开始都在数据库中，不在es中，所以刚开始查询，
     * 先看看es有没有数据，如果没有，就把数据从数据库同步到es中
     */
    public void initDatabaseToES(){
        Pageable pageable = PageRequest.of(0,5);
        Page<MobileUser> page = mobileUserEsMapper.findAll(pageable);
        if(page.getContent().isEmpty()){
            List<MobileUser> list = mobileUserMapper.getMobileUserList();
            list.forEach(s -> {
                mobileUserEsMapper.save(s);
            });
        }
    }
}
